package org.example.groupwarehouse.controller;

import jakarta.servlet.http.HttpSession;
import org.example.groupwarehouse.entity.Category;
import org.example.groupwarehouse.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import jakarta.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 分类列表
    @GetMapping("/category/list")
    public String list(Model model) {
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "admin/category_list"; // 确保模板文件存在
    }

    // 添加分类页面
    @GetMapping("/category/add")
    public String add(Model model) {
        model.addAttribute("category", new Category());
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "admin/category_add"; // 确保模板文件存在
    }

    // 添加分类
    @PostMapping("/category/add")
    public String addCategory(@Valid @ModelAttribute("category") Category category,
                              BindingResult result,
                              Model model,
                              RedirectAttributes redirectAttributes) {

        if (result.hasErrors()) {
            model.addAttribute("categories", categoryService.getAllCategories());
            return "admin/category_add";
        }

        try {
            categoryService.addCategory(category);
            redirectAttributes.addFlashAttribute("success", "分类添加成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }
        return "redirect:/admin/category/list";
    }

    // 修改分类页面
    @GetMapping("/category/edit/{id}")
    public String editCategory(@PathVariable Integer id, Model model) {
        Category category = categoryService.findById(id);
        if (category == null) {
            return "redirect:/admin/category/list?error=category_not_found";
        }
        model.addAttribute("category", category);
        model.addAttribute("categories", categoryService.getAllCategories());
        return "admin/category_edit"; // 确保模板文件存在
    }

    // 修改分类
    @PostMapping("/category/edit")
    public String updateCategory(@Valid @ModelAttribute("category") Category category,
                                 BindingResult result,
                                 Model model,
                                 RedirectAttributes redirectAttributes) {

        if (result.hasErrors()) {
            model.addAttribute("categories", categoryService.getAllCategories());
            return "admin/category_edit";
        }

        try {
            categoryService.updateCategory(category);
            redirectAttributes.addFlashAttribute("success", "分类修改成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }
        return "redirect:/admin/category/list";
    }

    // 删除分类
    @GetMapping("/category/delete/{id}")
    public String deleteCategory(@PathVariable Integer id,
                                 RedirectAttributes redirectAttributes) {

        System.out.println("id -------------- > : "+ id);
        try {
            if (categoryService.deleteCategory(id)) {
                redirectAttributes.addFlashAttribute("success", "分类删除成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "该分类下存在商品，无法删除");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }
        return "redirect:/admin/category/list";
    }



}